<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/1/25
 * Time: 下午7:19
 */

namespace App\Admin\Controllers;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;

use App\Http\Controllers\Controller;
use App\Models\FooterTab;
class FooterTabController extends Controller
{
    use ModelForm;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('千幻底部按钮配置');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新增千幻底部按钮');
            $content->description('');
            $content->body($this->form());
        });
    }
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑千幻底部按钮');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    protected function grid() {
        return Admin::grid(FooterTab::class, function (Grid $grid) {
            //$grid->id('ID')->sortable();
            $grid->tab_id('Tab_ID')->sortable();
            $grid->title('Tab名称');
            $grid->select_poster('选中图标')->display(function($img){
                $width = 80;
                $height = 40;
                if(strpos($img, 'http')===false) {
                    $img = config('admin.upload.host') .'/'. $img;
                }
                return "<img src='{$img}' width='{$width}' height='{$height}'/>";
            });
            $grid->unselect_poster('未选中图标')->display(function($img){
                $width = 80;
                $height = 40;
                if(strpos($img, 'http')===false) {
                    $img = config('admin.upload.host') .'/'. $img;
                }
                return "<img src='{$img}' width='{$width}' height='{$height}'/>";
            });

            $grid->show_order('显示顺序');
            $grid->flag('是否生效')->display(function($flag){
                if( $flag==1 ) return '有效';
                return '无效';
            });
            $grid->updated_at('更新时间');
            $grid->filter(function ($filter) {
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
        });
    }

    protected function form($mode='')
    {
        return Admin::form(FooterTab::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            }
            $form->text('tab_id','Tab_ID');
            $form->text('title','Tab名称');
            $form->image('select_poster','选中图标')->uniqueName()->removable();
            $form->image('unselect_poster','未选中图标')->uniqueName()->removable();
            $form->text('show_order', '展示顺序');
            $form->select('flag', '是否生效')->options([0=>'否', 1=>'是']);
        });
    }
}